package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping("/add")
    public ResponseEntity saveUser(@RequestBody User user){
        long saveUser = userService.saveUser(user);
        System.out.println(saveUser);
        return ResponseEntity.ok(saveUser);
    }

    @GetMapping("/findByMobile")
    public ResponseEntity findUser(String phone) {
        User user = userService.findUser(phone);
        return ResponseEntity.ok(user);
    }
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> paramMap){
        //获取手机号
        String phone = paramMap.get("phone");
        //发送验证码
        userService.sendValidateCode(phone);
        //响应结果
        return ResponseEntity.ok(null);
    }
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        String phone = paramMap.get("phone");
        String verificationCode = paramMap.get("verificationCode");
        //获取到手机号和验证码调用service进行校验
     Map<String,Object> result= userService.loginVerification(phone,verificationCode);
        //如果校验成功存入手机号和token
        return ResponseEntity.ok(result);
    }

}
